package com.goods.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.common.model.business.Product;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.system.PageVO;
import com.goods.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    ///findProductList?pageNum=1&pageSize=6&name=&categoryId=&supplier=&status=0
    @Override
    public PageVO findProductList(Map map) {
        String pageNum = (String) map.get("pageNum");
        String pageSize = (String) map.get("pageSize");
        String categoryId = (String) map.get("categoryId");
        if (!StringUtils.isEmpty(categoryId)) {
            String[] split = categoryId.split(",");
            if (split.length == 1) {
                map.put("one_category_id", split[0]);
            }
            if (split.length == 2) {
                map.put("two_category_id", split[1]);
            }
            if (split.length == 3) {
                map.put("three_category_id", split[2]);
            }
        }
        List<Product> list = productMapper.findProductList(map);
        if (null != list) {
            List<Product> page = ListPageUtils.page(list, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
            return new PageVO<>(list.size(),page);
        }
        return null;
    }

    @Override
    public Product edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }
}
